package com.tarena.lbs.coupon.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.coupon.dao.mapper.CouponMapper;
import com.tarena.lbs.coupon.dao.repository.CouponRepository;
import com.tarena.lbs.coupon.pojo.entity.Coupon;
import com.tarena.lbs.coupon.pojo.po.CouponPO;
import com.tarena.lbs.coupon.pojo.query.CouponQuery;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CouponRepositoryImpl implements CouponRepository {
    @Resource
    private CouponMapper couponMapper;
    @Override
    public PageInfo<CouponPO> pageList(CouponQuery query) {
        QueryWrapper<CouponPO> queryWrapper = assembleCouponQuery(query);
        //PageHelper.startPage 之间不能有代码
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<CouponPO> activityList = couponMapper.selectList(queryWrapper);
        //selectList 数据库操作 之间不能有代码
        return new PageInfo<>(activityList);
    }

    @Override
    public CouponPO selectById(Integer id) {
        return couponMapper.selectById(id);
    }

    @Override
    public Integer countUserCoupon(Integer userId, Integer couponId) {
        return couponMapper.selectById(userId, couponId);
    }

    @Override
    public Object getVerifyBaseUrl() {
        return couponMapper.getVerifyBaseUrl();
    }

    private QueryWrapper<CouponPO> assembleCouponQuery(CouponQuery query) {
        QueryWrapper<CouponPO> queryWrapper = new QueryWrapper<>();
        if (query != null){
            if (query.getCouponName() != null){
                queryWrapper.like("coupon_name",query.getCouponName());
            }
        }
        return queryWrapper;
    }
}
